
/**
 * <p>Title: StarListGUI</p>
 * <p>Description: CS 235 Assignment #3</p>
 * @author Erin McNeil
 * @email erin.mcneil@my.uwrf.edu
 * @date 12/2/2011
 * @team Erin, Dara, Alan
 */

package a3;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

/**
 * StarList Graphical User Interface
 */
public class StarListGUI extends JFrame implements ActionListener {
	/**
	 * Data members for the GUI interface
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X = 100, FRAME_Y = 100, FRAME_WIDTH = 400,
			FRAME_HEIGHT = 300;
	private final int SCROLL_X = 30, SCROLL_Y = 30, SCROLL_WIDTH = 300,
			SCROLL_HEIGHT = 200;
	private final int SORT_X = 30, SORT_Y = 235, SORT_WIDTH = 100,
			SORT_HEIGHT = 30;
	private String SORT_STRING = "Sort";
	private String SHUFFLE_STRING = "Shuffle";
	private JList listview;
	private JScrollPane scroll;
	private DefaultListModel listmodel;
	private Vector<String> list;

	private JButton sort;

	/**
	 * StarListGUI constructor
	 * 
	 * @require
	 * @ensure listmodel.getSize() == 21
	 * 
	 */
	public StarListGUI(String title) {
		super(title);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(null);

		/*
		 * Data that will be used as the "listmodel"
		 */
		String[] stars = { "Polaris Australis", "Pollux", "Rana", "Sirius",
				"Yed Prior", "Electra", "Gacrux", "Gianfar", "Maia", "Merga",
				"Nash", "Nodus Secundus", "Peacock", "Pherkad Minor",
				"Pleione", "Gienah Cygni", "Grafias", "Head of Hydrus",
				"Herschel's Garnet Star", "Hyadum II", "Kaus Borealis" };

		/*
		 * adding the elements of array stars into the listmodel
		 */
		listmodel = new DefaultListModel();
		for (String s : stars)
			listmodel.addElement(s);

		/*
		 * Now that the data is available, setting up GUI elements to display
		 * it.
		 */
		listview = new JList(listmodel);
		listview.setVisible(true);
		scroll = new JScrollPane(listview);
		scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
		add(scroll);

		sort = new JButton(SORT_STRING);
		sort.addActionListener(this);
		sort.setBounds(SORT_X, SORT_Y, SORT_WIDTH, SORT_HEIGHT);
		add(sort);

		setVisible(true);
	}

	// actionPerformed statements:

	/**
	 * actionPerformed - Method to either sort the list of stars or shuffle the
	 * list of stars. Depending if the user clicks the sort button or the
	 * shuffle button.
	 * 
	 * @require
	 * @ensure listmodel.getSize() == 21
	 * 
	 */
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();

		/*
		 * if the user clicks the sort button:
		 */
		if (source.equals(sort)) {
			if (sort.getText().equals(SORT_STRING)) {
				sort.setText(SHUFFLE_STRING);

				/*
				 * copying all the items in the listmodel into a Vector<String>
				 * called list
				 */

				list = new Vector<String>();
				list.add("Polaris Australis");
				list.add("Pollux");
				list.add("Rana");
				list.add("Sirius");
				list.add("Yed Prior");
				list.add("Electra");
				list.add("Gacrux");
				list.add("Gianfar");
				list.add("Maia");
				list.add("Merga");
				list.add("Nash");
				list.add("Nodus Secundus");
				list.add("Peacock");
				list.add("Pherkad Minor");
				list.add("Pleione");
				list.add("Gienah Cygni");
				list.add("Grafias");
				list.add("Head of Hydrus");
				list.add("Herschel's Garnet Star");
				list.add("Hyadum II");
				list.add("Kaus Borealis");

				/*
				 * deleting everything from the listmodel
				 */
				listmodel.clear();

				/*
				 * Sorting the Vector list
				 */
				Collections.sort(list);

				/*
				 * putting the Vector or ArrayList into the (now empty)
				 * listmodel
				 */
				for (String s : list)
					listmodel.addElement(s);

				/*
				 * If the user clicks the shuffle button.
				 */
			} else {
				if (sort.getText().equals(SHUFFLE_STRING)) {
					sort.setText(SORT_STRING);

					/*
					 * copying all the items in the listmodel into a Vector<String>
					 * called list
					 */

					list = new Vector<String>();
					list.add("Polaris Australis");
					list.add("Pollux");
					list.add("Rana");
					list.add("Sirius");
					list.add("Yed Prior");
					list.add("Electra");
					list.add("Gacrux");
					list.add("Gianfar");
					list.add("Maia");
					list.add("Merga");
					list.add("Nash");
					list.add("Nodus Secundus");
					list.add("Peacock");
					list.add("Pherkad Minor");
					list.add("Pleione");
					list.add("Gienah Cygni");
					list.add("Grafias");
					list.add("Head of Hydrus");
					list.add("Herschel's Garnet Star");
					list.add("Hyadum II");
					list.add("Kaus Borealis");

					/*
					 * deleting everything from the listmodel
					 */
					listmodel.clear();

					/*
					 * Shuffling the Vector named list
					 */
					Collections.shuffle(list);

					/*
					 * putting the Vector or ArrayList into the (now empty)
					 * listmodel
					 */
					for (String s : list)
						listmodel.addElement(s);
				}

			}
		}
	}

	// Main Method: 
	
	/**
	 * The Main Method
	 * 
	 * @require
	 * @ensure
	 */
	public static void main(String[] args) {
		new StarListGUI("My Star System");
	}
}
